package sm.utils;

public class Message {
	private String id, header, command, data;
	
	public Message(){
		this.id = null;
		this.header = null;
		this.command = null;
		this.data = null;
	}
	
	public void setId(String id){
		this.id = new String(id);

	}
	public void setHeader(String hd){
		this.header = new String(hd);

	}
	public void setCommand(String cmd){
		this.command = new String(cmd);

	}
	public void setData(String data){
		this.data = new String(data);
	}

	public String getId(){
		return this.id;
	}
	public String getHeader(){
		return this.header;
	}
	public String getCommand(){
		return this.command;
	}
	public String getData(){
		return this.data;
	}
	public boolean checkValidity(){
		return this.id!=null && this.header!=null && this.command!=null && this.data!=null;
	}
	
	public Message parse(String request){
		Message msg = new Message();
		String[] req = request.split(PropertySet._requestSplit);
		if(req.length!=4){
			return new Message();
		}else{
			msg.setId(req[0]);
			msg.setHeader(req[1]);
			msg.setCommand(req[2]);
			msg.setData(req[3]);
		}
		return msg;
	}
	public String toString(){
		return this.id + PropertySet._fieldSplit + this.header + PropertySet._fieldSplit +
				this.command + PropertySet._fieldSplit + this.data +'\n';
	}
}
